package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Atividade;
import models.cad.Funcionario;
import models.cad.Horario;
import models.cad.Turma;
import models.cad.Usuario;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

@SuppressWarnings("serial")
public class CadTurGUI extends GUIInternalFrame implements GUICadInterface{
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JFormattedTextField tfCodFun;
	public JFormattedTextField tfCodHor;
	public JFormattedTextField tfCodAtv;
	public JButton btNFun;
	public JButton btNHor;
	public JButton btNAtv;
	public JFormattedTextField tfAno;
	public JLabel lbFun;
	public JLabel lbHor;
	public JLabel lbAtv;
	
	public CadTurGUI() {
		setTitle("Cadastro de Turmas");
		setSize(new Dimension(330, 290));
		
		tfNome = new JTextField();
		tfNome.setBounds(83, 59, 212, 20);
		getContentPane().add(tfNome);
		tfNome.setColumns(10);
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(83, 28, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel lblNewLabel = new JLabel("Cod.:");
			lblNewLabel.setLabelFor(tfCod);
			lblNewLabel.setBounds(27, 31, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Nome:");
			lblNewLabel_1.setLabelFor(tfNome);
			lblNewLabel_1.setBounds(27, 62, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			JLabel lblAtiv = new JLabel("Atividade:");
			lblAtiv.setBounds(27, 124, 57, 14);
			getContentPane().add(lblAtiv);
			
			tfCodAtv = new JFormattedTextField(new MaskFormatter("###"));
			tfCodAtv.setColumns(3);
			tfCodAtv.setBounds(83, 121, 30, 20);
			getContentPane().add(tfCodAtv);
			
			lbAtv = new JLabel(".");
			lbAtv.setBounds(145, 124, 150, 14);
			getContentPane().add(lbAtv);
			
			JLabel lblAno = new JLabel("Ano:");
			lblAno.setBounds(27, 93, 34, 14);
			getContentPane().add(lblAno);
			
			tfAno = new JFormattedTextField(new MaskFormatter("####"));
			tfAno.setColumns(4);
			tfAno.setBounds(83, 90, 30, 20);
			getContentPane().add(tfAno);
			
			JLabel lblHorario = new JLabel("Horario:");
			lblHorario.setBounds(27, 155, 57, 14);
			getContentPane().add(lblHorario);
			
			tfCodHor = new JFormattedTextField(new MaskFormatter("###"));
			tfCodHor.setColumns(3);
			tfCodHor.setBounds(83, 152, 30, 20);
			getContentPane().add(tfCodHor);
			
			lbHor = new JLabel(".");
			lbHor.setBounds(145, 155, 150, 14);
			getContentPane().add(lbHor);
			
			JLabel lblProfessor = new JLabel("Professor:");
			lblProfessor.setBounds(27, 186, 57, 14);
			getContentPane().add(lblProfessor);
			
			tfCodFun = new JFormattedTextField(new MaskFormatter("###"));
			tfCodFun.setColumns(3);
			tfCodFun.setBounds(83, 183, 30, 20);
			getContentPane().add(tfCodFun);
			
			lbFun = new JLabel(".");
			lbFun.setBounds(145, 186, 150, 14);
			getContentPane().add(lbFun);
			
			btNAtv = new JButton("N");
			btNAtv.setBounds(115, 120, 23, 23);
			getContentPane().add(btNAtv);
			
			btNHor = new JButton("N");
			btNHor.setBounds(115, 151, 23, 23);
			getContentPane().add(btNHor);
			
			btNFun = new JButton("N");
			btNFun.setBounds(115, 182, 23, 23);
			getContentPane().add(btNFun);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(27, 218, 268, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(188, 1, 80, 23);
			panel_1.add(btListar);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfCodAtv.addFocusListener(fl);
        tfCodFun.addFocusListener(fl);
        tfCodHor.addFocusListener(fl);
        btNAtv.addActionListener(ac);
        btNFun.addActionListener(ac);
        btNHor.addActionListener(ac);
        btNAtv.addFocusListener(fl);
        btNFun.addFocusListener(fl);
        btNHor.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfAno.setText("");
		tfCodAtv.setText("");
		tfCodHor.setText("");
		tfCodFun.setText("");
		lbAtv.setText("");
		lbFun.setText("");
		lbHor.setText("");
		btExcluir.setEnabled(false);
	}

	public void setDados(Object obj){
		Turma tur = (Turma) obj;
		tfCod.setText(tur.getCodigo());
		tfNome.setText(tur.getNome());
		tfAno.setText(String.valueOf(tur.getAno()));
		tfCodAtv.setText(tur.getAtividade().getCodigo());
		tfCodHor.setText(tur.getHorario().getCodigo());
		tfCodFun.setText(tur.getFuncionario().getCodigo());
	}
	
	public Turma getDados(){
		Turma tur = new Turma();
		tur.setCodigo(tfCod.getText());
		tur.setNome(tfNome.getText());
		tur.setAno(Integer.valueOf(tfAno.getText()));
		tur.setAtividade(Atividade.getAtividade(tfCodAtv.getText()));
		tur.setHorario(Horario.getHorario(tfCodHor.getText()));
		tur.setFuncionario(Funcionario.getFuncionario(tfCodFun.getText()));
		tur.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		tur.setDtOpe(new Date(System.currentTimeMillis()));
		return tur;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}
}
